package  
{
	import com.bit101.components.Accordion;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.geom.Point;
	import samples.BaseSample;
	import systems.spidron.Spidron;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class SpidronSample_2 extends BaseSample 
	{
		private var iterations:int;
		private var size:Number;
		private var step:Number = Math.PI / 3;
		private var multiX:Number = Math.sin( step );
		private var multiY:Number = Math.cos( step );
		
		[Embed(source = '../lib/lemur.jpg')]private var texture:Class;
		private var bd:BitmapData = new texture().bitmapData;
		
		//private var colors:Vector.<uint> = Vector.<uint>( [  0x701604 , 0xAB3C0C ] );//0x3B1E26,, 0xD96B2F, 0xFF996E ] );
		//private var colors:Vector.<uint> = Vector.<uint>( [ 0x57ADFF, 0x0F3D69 , 0x2C72B5 ] );//, 0xD96B2F, 0xFF996E ] );
		private var colors:Vector.<uint> = Vector.<uint>( [ 0x57ADFF, 0x0F3D69 , 0x2C72B5, 0x402A38 , 0xC6D9B8 , 0x80A694 , 0x657371 ] );//, 0xD96B2F, 0xFF996E ] );
		private var colId:int = 0;
		private var iter:int;
		public function SpidronSample_2() 
		{
			
			size = 150;
			iterations = 10;
			iter = 0;
			
			var w:int = 1366 / size + 1;
			var h:int = 768 / size + 1;
			var ox:Number, oy:Number;
			rosace( sw / 2, sh / 2 );
			
			//
			//for ( var i:int = 0; i < w; i++ )
			//{
				//for ( var j:int = 0; j < h; j++ )
				//{
					//
					//ox = i * 2;
					//if( j % 2 == 0 )ox += 1
					//oy = j * 3;
					//rosace( ox * ( size * multiX ), oy * ( size * multiY ) , (j % 2 == 0 && i % 2 == 1)?1: -1 );
					//
				//}
			//}
			
			
			bd = new BitmapData( 1366, 768, false, 0 );
			bd.draw( this );
			var bmp:Bitmap = new Bitmap( bd );
			TestGrain.compute( bmp, .5, .3, true );
			addChild( bmp );
			
		}
		
		private function rosace( x:Number, y:Number, dir:int = 1 ):void 
		{
			
			
			var i:Number;
			var sp:Spidron = new Spidron( x, y, size, 0, 30 / 180 * Math.PI )
				
			for ( i = 0; i < Math.PI * 2; i += step )
			{
				
				sp.reset();
				
				sp.x = x + Math.cos( i ) * ( size * multiX );
				sp.y = y + Math.sin( i ) * ( size * multiX );
				sp.angle = i + Math.PI / 2 * ( ( iter%2==0 ) ? 1:-1 );// * dir
				
				sp.build( iterations );
				draw( sp, dir );
				
				
				sp.reset();
				
				sp.x = x + Math.cos( i ) * ( size * multiX );
				sp.y = y + Math.sin( i ) * ( size * multiX );
				sp.angle = i + Math.PI / 2 + ( ( iter%2==0 ) ? Math.PI:0 )   ;// * dir
				
				sp.build( iterations );
				draw( sp, dir );
				
				colId++;
				
				iter++;
				
			}
			
			
		}
		
		private function draw( spidron:Spidron, iter:int ):void
		{
			var i:int;
			var a:Point, b:Point, c:Point, d:Point;
			
			var vertices:Vector.<Number> = new Vector.<Number>();
			var uvs:Vector.<Number> = new Vector.<Number>();
			var indices:Vector.<int> = new Vector.<int>();
			
			var stepy:Number = 1 / iterations;
			
			for ( i = 0; i < spidron.points.length; i+= 4 )
			{
				a = spidron.points[ i ];
				b = spidron.points[ i + 1 ];
				c = spidron.points[ i + 2 ];
				d = spidron.points[ i + 3 ];
				
				vertices.push( a.x, a.y, b.x, b.y, c.x, c.y, d.x, d.y );
				
				indices.push( 	i, i + 1, i + 2,
								i, i + 2, i + 3	);
				 
				uvs.push( 	0, 0, 
							1, 0,
							1, 1,
							0, 1
							);
				/*
				*/
				//graphics.lineStyle( 0, colors[ colId ] );
				//graphics.beginFill( colors[ int( Math.random() * colors.length ) ] );
				//graphics.beginFill( colors[ colId ] );
				
				
				graphics.lineStyle( 0, 0x333333 );
				drawTri( a, b, c, graphics );
				drawTri( a, c, d, graphics );
				
				graphics.lineStyle( 0, 0xDDDDDD );
				drawQuad( a, b, c, d, graphics );
			}
			
			//bd.fillRect( bd.rect, colors[ iter % colors.length ] );
			
			//if ( iter <= 0 )colId++
			
			//graphics.beginBitmapFill( bd, null, false, true ); 
			//graphics.drawTriangles( vertices, indices, uvs );
			
		}
		
		private function drawTri(a:Point, b:Point, c:Point, graphics:Graphics):void 
		{
			graphics.moveTo( a.x, a.y );
			graphics.lineTo( b.x, b.y );
			graphics.lineTo( c.x, c.y );
			graphics.lineTo( a.x, a.y );
		}
		
		private function drawQuad( a:Point, b:Point, c:Point, d:Point, graphics:Graphics):void 
		{
			graphics.moveTo( b.x, b.y );
			graphics.lineTo( c.x, c.y );
			graphics.moveTo( a.x, a.y );
			graphics.lineTo( d.x, d.y );
			
		}
	}

}